package org.rayken.trix.commons.game;

import java.util.ArrayList;
import java.util.List;

import org.rayken.trix.commons.card.Card;
import org.rayken.trix.commons.card.Suit;

/**
 * A Step in a Run
 * @author E-Kraiem-A
 *
 */
public class Trick {
	private List<Play> plays = new ArrayList<Play>();
	private Run run;
	
	
	
	Trick(Run run) {
		this.run = run;
	}
	
	public List<Play> getPlays() {
		return plays;
	}

	public Run getRun() {
		return run;
	}
	
	public boolean isComplete(){
		return plays.size() == 4;
	}
	
	public Play getPlayForCard(Card card){
		for (Play p : plays)
			if (p.getCard() == card)
				return p;
		return null;
	}
	
	public boolean isCardPlayed(Card card){
		return getPlayForCard(card) != null;
	}

	public Play getWiningPlay(){
		if (run.getType() == RunType.TRIX)
			throw new IllegalStateException("there is no winning play in trix");
		if (plays.size() == 0)
			throw new IllegalStateException("there is no winning play");
		Play firstPlay = plays.get(0);
		Suit mainSuite = firstPlay.getCard().getSuit();
		
		Play maxPlay = firstPlay;
		for(Play p : plays){
			if (p.getCard().getSuit() == mainSuite && p.getCard().getRank().getRank() > maxPlay.getCard().getRank().getRank())
				maxPlay = p;
		}
		return maxPlay;
	}
	
	public Play nextPlay(Player player) {
		Play play = new Play(player, this);
		plays.add(play);
		return play;
	}
}
